/*
 *  linux/kernel/system_call.s
 *
 *  (C) 1991  Linus Torvalds
 */

/*
 *  system_call.s  contains the system-call low-level handling routines.
 * This also contains the timer-interrupt handler, as some of the code is
 * the same. The hd- and flopppy-interrupts are also here.
 *
 * NOTE: This code handles signal-recognition, which happens every time
 * after a timer-interrupt and after each system call. Ordinary interrupts
 * don't handle signal-recognition, as that would clutter them up totally
 * unnecessarily.
 *
 * Stack layout in 'ret_from_system_call':
 *
 *	 0(%esp) - %eax
 *	 4(%esp) - %ebx
 *	 8(%esp) - %ecx
 *	 C(%esp) - %edx
 *	10(%esp) - %fs
 *	14(%esp) - %es
 *	18(%esp) - %ds
 *	1C(%esp) - %eip
 *	20(%esp) - %cs
 *	24(%esp) - %eflags
 *	28(%esp) - %oldesp
 *	2C(%esp) - %oldss
 */

SIG_CHLD	= 17

EAX		= 0x00
EBX		= 0x04
ECX		= 0x08
EDX		= 0x0C
FS		= 0x10
ES		= 0x14
DS		= 0x18
EIP		= 0x1C
CS		= 0x20
EFLAGS		= 0x24
OLDESP		= 0x28
OLDSS		= 0x2C

state	= 0		# these are offsets into the task-struct.
counter	= 4
priority = 8
signal	= 12
sigaction = 16		# MUST be 16 (=len of sigaction)
blocked = (33*16)

# offsets within sigaction
sa_handler = 0
sa_mask = 4
sa_flags = 8
sa_restorer = 12

// 系统调用的数量
nr_system_calls = 73

/*
 * Ok, I get parallel printer interrupts while using the floppy for some
 * strange reason. Urgel. Now I just ignore them.
 */
.globl system_call,sys_fork,timer_interrupt,sys_execve
.globl hd_interrupt,floppy_interrupt,parallel_interrupt
.globl device_not_available, coprocessor_error

.align 2
bad_sys_call:
	# 系统调用号超范围，返回-1
	movl $-1,%eax
	iret
.align 2
reschedule:
	# 下面使用的是先压栈，然后跳转至schedule
	# 这样相当于将：构造了一个函数
	# void ret_from_sys_call()
	#      --- schedler()   在刚进入ret_from_sys_call的第一条指令就调用schedule
	# 好像没有太大必要
	# 可以call schedule，然后再jmp $ret_from_sys_call
	pushl $ret_from_sys_call
	jmp schedule

	
.align 2
system_call:
	# 判断系统调用号是否超过最大索引，如果超过显示就出错了。
	cmpl $nr_system_calls-1,%eax
	ja bad_sys_call  # 直接回-1，结束

	# 保存上下文，只需要考虑保存自己有修改的
	# si, di, bp没有保存
	push %ds		# 以下3个段有发生切换，为什么？所有需要保存
	push %es
	push %fs

	pushl %edx
	pushl %ecx		# push %ebx,%ecx,%edx as parameters
	pushl %ebx		# to the system call

	# 切换进内核的数据段，使用内核段进行访问内核空间
	movl $0x10,%edx		# set up ds,es to kernel space
	mov %dx,%ds
	mov %dx,%es

	# 将fs设置成局部数据空间，即用户的数据空间, LDT中的数据段, 选择子为0x10, 权限3，LDT表中索引
	movl $0x17,%edx		# fs points to local data space
	mov %dx,%fs

	# 调用系统调用
	call *sys_call_table(,%eax,4)
	# 调用完成后，eax保存的返回值，所以下载使用eax要保存
	pushl %eax

	# 调用系统调用后，当前任务可能进入就绪状态，或者因为其它任务暂停运行
	# 因此，此处需要判断一下。即其在退出系统调用时切换处理
	# if curr->state != 0 (=0表示就绪态，非0则为停止状态，见sched.h)
	#    goto reschedule
	# else if curr->count == 0 
	#    goto reschedule
	# ret_from_sys_call:   
	movl current,%eax
	cmpl $0,state(%eax)		# state
	jne reschedule			# 不为0，即当前任务非就绪态，不能再运行了，需要挂起运行其它任务
	cmpl $0,counter(%eax)		# 如果时间片计数也为0，则时间表用完，岂也进行重新调试
	je reschedule

	# 执行完系统调用，返回
ret_from_sys_call:
	# 下面进行信号处理，但如果是任务0，则不应当进行这方便的处理，因为比较特殊其不能接受任何信号
	# if current == task + 0
	#     goto 3f			
	movl current,%eax		# task[0] cannot have signals
	cmpl task,%eax			
	je 3f				# 任务0，不做下面的信号处理，直接退出

	# 检查cs和ss来判断是否是用户任务进入到的系统调用。不是则直接退出
	# 检查是否RPL=3，LDT=1 ？？ TODO: 为何要这样检查？？还有核心任务？？
	cmpw $0x0f,CS(%esp)		# was old code segment supervisor ?
	jne 3f
	cmpw $0x17,OLDSS(%esp)		# was stack segment = 0x17 ?
	jne 3f

	# 以下进行信号处理。后续处理。。。
	movl signal(%eax),%ebx
	movl blocked(%eax),%ecx
	notl %ecx
	andl %ebx,%ecx
	bsfl %ecx,%ecx
	je 3f
	btrl %ecx,%ebx
	movl %ebx,signal(%eax)
	incl %ecx

	pushl %ecx		# 压栈信号值，然后调用
	call do_signal
	popl %eax		# 调用完成后出栈
	
	# 恢复上下文
3:	popl %eax
	popl %ebx
	popl %ecx
	popl %edx
	pop %fs
	pop %es
	pop %ds
	iret

.align 2
coprocessor_error:
	push %ds
	push %es
	push %fs
	pushl %edx
	pushl %ecx
	pushl %ebx
	pushl %eax
	movl $0x10,%eax
	mov %ax,%ds
	mov %ax,%es
	movl $0x17,%eax
	mov %ax,%fs
	pushl $ret_from_sys_call
	jmp math_error

.align 2
device_not_available:
	push %ds
	push %es
	push %fs
	pushl %edx
	pushl %ecx
	pushl %ebx
	pushl %eax
	movl $0x10,%eax
	mov %ax,%ds
	mov %ax,%es
	movl $0x17,%eax
	mov %ax,%fs
	pushl $ret_from_sys_call
	clts				# clear TS so that we can use math
	movl %cr0,%eax
	testl $0x4,%eax			# EM (math emulation bit)
	je math_state_restore
	pushl %ebp
	pushl %esi
	pushl %edi
	call math_emulate
	popl %edi
	popl %esi
	popl %ebp
	ret

.align 2
timer_interrupt:
	push %ds		# save ds,es and put kernel data space
	push %es		# into them. %fs is used by _system_call
	push %fs
	pushl %edx		# we save %eax,%ecx,%edx as gcc doesn't
	pushl %ecx		# save those across function calls. %ebx
	pushl %ebx		# is saved as we use that in ret_sys_call
	pushl %eax

	# 切换到内核数据段
	movl $0x10,%eax
	mov %ax,%ds
	mov %ax,%es
	movl $0x17,%eax
	mov %ax,%fs

	# 增加系统时钟节拍计数
	incl jiffies

	# 发EOI命令，通知中断已经响应，可以继续定时产生中断了
	movb $0x20,%al		# EOI to interrupt controller #1
	outb %al,$0x20

	movl CS(%esp),%eax
	andl $3,%eax		# %eax is CPL (0 or 3, 0=supervisor)
	pushl %eax
	call do_timer		# 'do_timer(long CPL)' does everything from
	addl $4,%esp		# task switching to accounting ...
	jmp ret_from_sys_call

.align 2
sys_execve:
	lea EIP(%esp),%eax
	pushl %eax
	call do_execve
	addl $4,%esp
	ret

.align 2
sys_fork:
	call find_empty_process
	testl %eax,%eax
	js 1f
	push %gs
	pushl %esi
	pushl %edi
	pushl %ebp
	pushl %eax
	call copy_process
	addl $20,%esp
1:	ret

hd_interrupt:
	pushl %eax
	pushl %ecx
	pushl %edx
	push %ds
	push %es
	push %fs
	movl $0x10,%eax
	mov %ax,%ds
	mov %ax,%es
	movl $0x17,%eax
	mov %ax,%fs
	movb $0x20,%al
	outb %al,$0xA0		# EOI to interrupt controller #1
	jmp 1f			# give port chance to breathe
1:	jmp 1f
1:	xorl %edx,%edx
	xchgl do_hd,%edx
	testl %edx,%edx
	jne 1f
	movl $unexpected_hd_interrupt,%edx
1:	outb %al,$0x20
	call *%edx		# "interesting" way of handling intr.
	pop %fs
	pop %es
	pop %ds
	popl %edx
	popl %ecx
	popl %eax
	iret

floppy_interrupt:
	pushl %eax
	pushl %ecx
	pushl %edx
	push %ds
	push %es
	push %fs
	movl $0x10,%eax
	mov %ax,%ds
	mov %ax,%es
	movl $0x17,%eax
	mov %ax,%fs
	movb $0x20,%al
	outb %al,$0x20		# EOI to interrupt controller #1
	xorl %eax,%eax
	xchgl do_floppy,%eax
	testl %eax,%eax
	jne 1f
	movl $unexpected_floppy_interrupt,%eax
1:	call *%eax		# "interesting" way of handling intr.
	pop %fs
	pop %es
	pop %ds
	popl %edx
	popl %ecx
	popl %eax
	iret

# 并口中断，什么都不做，只是发送EOI
# 为什么是0x20?
parallel_interrupt:
	pushl %eax
	movb $0x20,%al
	outb %al,$0x20
	popl %eax
	iret
